﻿//动态效果
window.onload = function () {
    let config = {
        vx: 4,
        vy: 4,
        height: 2,
        width: 2,
        count: 100,
        color: "121, 162, 185",
        stroke: "100, 200, 180",
        dist: 6000,
        e_dist: 20000,
        max_conn: 10
    };
    CanvasParticle(config);
};
//angular js
angular.module("login", []).controller("loginView", function ($scope) {
    //验证用户名
    $scope.$watch('username', function (newValue, oldValue) {
        //用户名
        let username = $scope.username;
        //新输入的用户名已旧用户名作比较
        if (username != oldValue) {
            if (username == "" || username == null) {
                $scope.usuccess = "请输入用户名";
                $("#state").html("<span style=\"width: 10px;color: red\">" + $scope.usuccess + "</span>");
                return;
            } else if (!(/^[a-zA-Z0-9_-]{4,16}$/.test(username))) {
                $scope.usuccess = "请输入4到16位用户名";
                $("#state").html("<span style=\"width: 10px;color: red\">" + $scope.usuccess + "</span>");
                return;
            } else {
                $scope.usuccess = "";
                $("#state").html("<span style=\"width: 10px;color: red\">" + $scope.usuccess + "</span>");
            }
        }
    });
    //验证密码
    $scope.$watch('password', function (newValue, oldValue) {
        //密码
        let password = $scope.password;
        //新输入的密码已旧密码名作比较
        if (password != oldValue) {
            if (password == "" || password == null) {
                $scope.rsuccess = "请输入密码";
                $("#states").html("<span style=\"width: 10px;color: red\">" + $scope.rsuccess + "</span>");
                return;
            } else if (!/^\w{5,10}$/.test(password)) {
                $scope.rsuccess = "请输入5到10位密码";
                $("#states").html("<span style=\"width: 10px;color: red\">" + $scope.rsuccess + "</span>");
                return;
            } else {
                $scope.rsuccess = "";
                $("#states").html("<span style=\"width: 10px;color: red\">" + $scope.rsuccess + "</span>");
            }
        }
    });
    //读取cookie
    let user = $.cookie('localhost2020');
    let pwd = $.cookie('localhost2021');
    $(document).ready(function () {
        // 判断是否存在cookie
        if (user) {
            $scope.username = user;
            $scope.password = pwd;
            $("input:text").val(user);
            $("input:password").val(pwd);
            $(".username").css("display", "none");
            $(".password").css("display", "none");
            $("#someCheckboxId").attr("checked", true);
        }
    });
    //单选框判断值
    let isCheckBox;
    //记住密码
    $scope.addGroupLinkMan = function ($event) {
        //获取标签对象
        let checked = $event.target;
        let addcheckArr = [];
        if (checked.checked) {
            //选中状态请求保存密码
            addcheckArr.push(checked.value);
            isCheckBox = checked.value;
        } else {
            //未选中状态取消保存密码
            addcheckArr.splice(checked.id, 1);
            isCheckBox = checked.id;
            // 删除cookie
            $.cookie('localhost2020', '');
            $.cookie('localhost2021', '');
        }
    };
    //点击登录
    $scope.sub = function () {
        //用户名
        let username = $scope.username;
        //密码
        let password = $scope.password;
        //判断账户或密码有没有输入
        if (username == "" || username == null) {
            $scope.usuccess = "请输入用户名";
            $("#state").html("<span style=\"width: 10px;color: red\">" + $scope.usuccess + "</span>");
            return;
        } else if (password == "" || password == null) {
            $scope.rsuccess = "请输入密码";
            $("#states").html("<span style=\"width: 10px;color: red\">" + $scope.rsuccess + "</span>");
            return;
        }
        //登录
        if ((username != null && password != null) && (username != "" && password != "") && ($scope.rsuccess == null || $scope.rsuccess == "") && ($scope.usuccess == null || $scope.usuccess == "")) {
            //封装用户名密码
            let loginData = {
                "username": username,
                "password": password
            };
            $.ajax({
                url: "http://localhost:8989/hospital/User/Login",
                type: "post",
                data: loginData,
                dataType: "text",
                success: function (success) {
                    if (success == "用户名不存在") {
                        // 删除cookie
                        $.cookie('localhost2020', '');
                        $.cookie('localhost2021', '');
                        $("#state").html("<span style=\"width: 10px;color: red\">" + success + "</span>");
                        return;
                    } else if (success == "用户名错误") {
                        // 删除cookie
                        $.cookie('localhost2020', '');
                        $.cookie('localhost2021', '');
                        $("#state").html("<span style=\"width: 10px;color: red\">" + success + "</span>");
                        return;
                    } else if (success == "密码错误") {
                        // 删除cookie
                        $.cookie('localhost2020', '');
                        $.cookie('localhost2021', '');
                        $("#states").html("<span style=\"width: 10px;color: red\">" + success + "</span>");
                        return;
                    } else if (success == "true") {
                        location.href = "分组管理.html";
                    }
                    //保存密码
                    if (isCheckBox == 'on') {
                        // 创建一个cookie并设置有效时间为 1天
                        $.cookie('localhost2020', $("input:text").val(), {expires: 1});
                        $.cookie('localhost2021', $("input:password").val(), {expires: 1});
                        $("#someCheckboxId").attr("checked", true);
                    }
                },
                error: function () {
                    $("#statess").html("<span style=\"width: 10px;color: red\">" + "登录出错" + "</span>");
                }
            });

        }
    }
})